package fc.testmain;

import cn.hutool.http.HttpRequest;
import fc.FcHttpCon;
import fc.entity.OaEntity;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.upgradetool.wscheck.Util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class testnyz {
    public static void main(String[] args) {
        String API_NAME="ZH_LSCG_XQ_NYZ0814";

        BaseBean log = new BaseBean();

        log.writeLog("接口"+API_NAME+"开始调用-----------------------------------");

        OaEntity oe=new OaEntity();

        //String requestid = info.getRequestid();
        String requestid = "1272156";
        //String tablename = info.getRequestManager().getBillTableName();
        String tablename = "formtable_main_643";
        oe.setREQUESTID(requestid);

        String sql= "SELECT requestid,BH,' ' SYDH,项目编号 XMBH,实验名称 SYMC,ccbm 产出部门,sqry ,rem from(select requestid,qgdh BH,pmsxmh 项目编号,jxmc 实验名称," +
                "' ' ccbm,hth sqry,' ' rem " +
                " from " + tablename + "  o1    where requestid=" + requestid + ")t";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if(rs.next()){
            oe.setREQUESTID(Util.null2String(rs.getString("requestid")));

            oe.setGCAH(Util.null2String(rs.getString("BH")));
            oe.setSYMC(Util.null2String(rs.getString("SYMC")));

            oe.setXMBH(Util.null2String(rs.getString("XMBH")));
            oe.setREQUESTID(Util.null2String(rs.getString("REQUESTID")));
            oe.setCCBM(Util.null2String(rs.getString("产出部门")));
            oe.setUSERID(Util.null2String(rs.getString("sqry")));
            oe.setREM(Util.null2String(rs.getString("rem")));

            if("".equals(oe.getCCBM())){oe.setCCBM("");}
        }

        String sql1= "select 料件编号 LJBH,数量 SL,单位 DW,物料需求时间 WLXQSJ,REM from "+
                "(select o1.id, wlph 料件编号,xqsl 数量,dw 单位, case when o2.wlqtxqsj <cast(getdate() as date ) then cast(getdate() as date ) else o2.wlqtxqsj end  " +
                "物料需求时间 ,wlmc REM from"+ tablename+"_dt1 o1  ,"+ tablename+
                " where o1.mainid=o2.id and o2.requestid="+requestid+")  tt  order by id";

        RecordSet rs1 = new RecordSet();
        rs1.execute(sql1);
        while(rs1.next()){
            List<OaEntity> dts=oe.getDT1();
            OaEntity dt1=new OaEntity();

            dt1.setLJBH(Util.null2String(rs1.getString("LJBH")));
            dt1.setSL(Util.null2String(rs1.getString("SL")));
            dt1.setDW(Util.null2String(rs1.getString("DW")));
            dt1.setWLXQSJ(Util.null2String(rs1.getString("WLXQSJ")));
            dt1.setREM(Util.null2String(rs1.getString("REM")));


            dts.add(dt1);
            oe.setDT1(dts);
        }


        String testparam="";
        log.writeLog("接口"+API_NAME+"参数开始-----------------------------------"+oe.getREQUESTID());

        log.writeLog("main-----------------------------------");
        log.writeLog("工程案号 GCAH:"+oe.getGCAH()+",实验单号 SYDH:"+oe.getSYDH());
        testparam="";
        log.writeLog("dt1-----------------------------------");


        for(int i=0;i<oe.getDT1().size();i++){
            log.writeLog("料件编号 LJBH:"+oe.getDT1().get(i).getLJBH()+
                    ",数量 SL:"+oe.getDT1().get(i).getSL()+
                    ",单位 DW:"+oe.getDT1().get(i).getDW()+
                    ",物料需求时间 WLXQSJ:"+oe.getDT1().get(i).getWLXQSJ()
            );
            oe.setWLXQSJ(oe.getDT1().get(i).getWLXQSJ());
            if (!"".equals(testparam)){
                testparam=testparam+",";
            }
            testparam+=
                    "{"+
                            "\"idx\": \""+(i+1)+"\","+
                            "\"code\": \""+oe.getDT1().get(i).getLJBH()+"\","+
                            "\"requst_date\": \""+oe.getDT1().get(i).getWLXQSJ()+"\","+
                            "\"amount\": "+oe.getDT1().get(i).getSL()+","+
                            "\"project\": \""+oe.getXMBH()+"\","+
                            "\"memo\": \""+oe.getDT1().get(i).getREM()+"\""+
                            "}";


        }


        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String  timestamp= format.format(new Date());


        DateFormat formataa = new SimpleDateFormat("yyyy-MM-dd");

        String createdate = formataa.format(new Date());

        String creater=oe.getUSERID();



        String json_param=
                "{"+
                        "\"key\": \"f5458f5c0f9022db743a7c0710145903\","+
                        "\"type\": \"sync\","+
                        "\"host\": {"+
                        "\"prod\": \"oa\","+
                        "\"ip\": \"192.168.0.1\","+
                        "\"lang\": \"zh_CN\","+
                        "\"acct\": \"tiptop\","+
                        "\"timestamp\": \""+timestamp+"\""+
                        "},"+
                        "\"service\": {"+
                        "\"prod\": \"T100\","+
                        "\"name\": \"apst300_crt\","+
                        "\"ip\": \"192.168.0.2\","+
                        "\"id\": \"topprd\""+
                        "},"+
                        "\"datakey\": {"+
                        "\"EntId\": \"23\","+
                        "\"CompanyId\": \"ZH00\""+
                        "},"+
                        "\"payload\": {"+
                        "\"std_data\": {"+
                        "\"parameter\": {"+
                        "\"p1\": \"\","+
                        "\"p2\": \"\""+
                        "}"+
                        "},"+
                        "\"param\": {"+
                        "\"top\": {"+
                        "\"user_code\": \""+creater+"\","+
                        "\"source_doc\": \""+oe.getREQUESTID()+"\","+
                        "\"memo\": \""+oe.getREM()+"\""+
                        "},"+
                        "\"data\": ["+
                        testparam+
                        "]"+
                        "}"+
                        "}"+
                        "}";


        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(FcHttpCon.ZH_YFSY_XQ).body(json_param).execute().body();
        System.out.println(data);
        String result="ERROR";
        System.out.println("----------------");
    }
}
